<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="filters.name" placeholder="名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button v-if="canAddTag" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="handleAdd">添加剧本类别</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      border
      fit
      :height="tableHeight"
      :data="list"
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column type="index" width="60" />
      <el-table-column prop="name" label="名称" min-width="180" />
      <el-table-column prop="price" label="价格(元)" min-width="120" :formatter="formatPrice" />
      <el-table-column prop="openPrice" label="开放价格(元)" min-width="120" :formatter="formatOpenPrice" />
      <el-table-column prop="todayPrice" label="当天价格(元)" min-width="120" :formatter="formatTodayPrice" />
      <el-table-column prop="returnMoneySupport" label="是否支持返利" min-width="160" :formatter="formatReturnMoneySupport" />
      <el-table-column prop="enableHolidayPrice" label="是否启用假期价格" min-width="160" :formatter="formatEnableHolidayPrice" />
      <el-table-column label="操作" width="680" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.index, scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--新增界面-->
    <el-dialog title="剧本类别信息" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form ref="addForm" :model="addForm" :label-width="formLabelWidth" :rules="addFormRules">
        <el-form-item label="名称" prop="name">
          <el-input v-model="addForm.name" auto-complete="off" />
        </el-form-item>
        <el-form-item label="价格(元)" prop="price">
          <el-input-number v-model="addForm.price" :min="0" controls-position="right" @keyup.enter.native="addSubmit" />
        </el-form-item>
        <el-form-item label="开放价格(元)" prop="openPrice">
          <el-input-number v-model="addForm.openPrice" :min="0" controls-position="right" @keyup.enter.native="addSubmit" />
        </el-form-item>
        <el-form-item label="是否支持返利" prop="price">
          <el-switch
            v-model="addForm.returnMoneySupport"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="是否启用假期价格" prop="price">
          <el-switch
            v-model="addForm.enableHolidayPrice"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false">取消</el-button>
        <el-button type="primary" :loading="addLoading" @click="addSubmit">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMovieLevel, saveMovieLevel, modifyMovieLevel } from '@/api/movie-level'
import waves from '@/directive/waves' // waves directive
import { mapGetters } from 'vuex'
import util from '@/utils/util'

export default {
  name: 'MovieLevels',
  directives: { waves },
  data() {
    return {
      tableHeight: 260,
      list: null,
      listLoading: true,
      filters: {
        name: ''
      },
      // 新增界面是否显示
      addFormVisible: false,
      addLoading: false,
      addFormRules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      },
      // 新增界面数据
      recordId: 0,
      addForm: {
        name: '',
        price: 0,
        openPrice: 0,
        version: 1,
        returnMoneySupport: null,
        enableHolidayPrice: null
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'formLabelWidth']),
    canAddTag() {
      const roles = this.userInfo.roles || []
      return roles.indexOf('Admin') !== -1
    }
  },
  created() {
    this.tableHeight = window.innerHeight - 160
    this.loadPageList()
  },
  methods: {
    formatPrice(row) {
      return util.currency.format(row.price)
    },
    formatOpenPrice(row) {
      return util.currency.format(row.openPrice)
    },
    formatTodayPrice(row) {
      return util.currency.format(row.todayPrice)
    },
    formatReturnMoneySupport(row) {
      if (row.returnMoneySupport) {
        return '是'
      }
      return '否'
    },
    formatEnableHolidayPrice(row) {
      if (row.enableHolidayPrice) {
        return '是'
      }
      return '否'
    },
    loadPageList() {
      this.listLoading = true
      const param = { ...this.filters }
      listMovieLevel(param).then(({ success, message, data }) => {
        this.listLoading = false
        if (success) {
          this.list = data
        } else {
          this.$message({
            message: message || '查询列表失败',
            type: 'error'
          })
        }
      }, () => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.loadPageList()
    },
    sortChange({ prop, order }) {
      this.handleFilter()
    },
    // 显示新增界面
    handleAdd: function() {
      this.addFormVisible = true
      this.addForm.name = ''
      this.addForm.price = 0
      this.addForm.openPrice = 0
      this.addForm.returnMoneySupport = 1
      this.addForm.enableHolidayPrice = 1
      this.recordId = null
      this.$nextTick(() => {
        this.$refs['addForm'].clearValidate()
      })
    },
    handleEdit(index, row) {
      this.addFormVisible = true
      this.recordId = row.id
      this.addForm.name = row.name
      this.addForm.price = row.price / 100
      this.addForm.openPrice = row.openPrice / 100
      this.addForm.version = row.version
      this.addForm.returnMoneySupport = row.returnMoneySupport
      this.addForm.enableHolidayPrice = row.enableHolidayPrice
      this.$nextTick(() => {
        this.$refs['addForm'].clearValidate()
      })
    },
    // 新增
    addSubmit: function() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true
            if (this.recordId) {
              this.doEdit()
            } else {
              this.doAdd()
            }
          })
        }
      })
    },
    doEdit() {
      const param = { ...this.addForm }
      param.price = parseInt((param.price * 100) + '')
      param.openPrice = parseInt((param.openPrice * 100) + '')
      modifyMovieLevel(this.recordId, param).then(({ success, message }) => {
        this.addLoading = false
        if (success) {
          this.$message({
            message: message || '提交成功',
            type: 'success'
          })
          this.$refs['addForm'].resetFields()
          this.addFormVisible = false
          this.loadPageList()
        } else {
          this.$message({
            message: message || '保存失败',
            type: 'error'
          })
        }
      }, () => {
        this.addLoading = false
      })
    },
    doAdd() {
      const param = { ...this.addForm }
      param.price = parseInt((param.price * 100) + '')
      param.openPrice = parseInt((param.openPrice * 100) + '')
      saveMovieLevel(param).then(({ success, message }) => {
        this.addLoading = false
        if (success) {
          this.$message({
            message: message || '提交成功',
            type: 'success'
          })
          this.$refs['addForm'].resetFields()
          this.addFormVisible = false
          this.loadPageList()
        } else {
          this.$message({
            message: message || '保存失败',
            type: 'error'
          })
        }
      }, () => {
        this.addLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
